//
//  GroupedIconView.swift
//  CircleTest1
//
//  Created by leeqb on 2/23/16.
//  Copyright © 2016 powerlee. All rights reserved.
//

import UIKit

class GroupedIconView: UIView {
    var imagePaths: [String]! {
        didSet {
            let halfWidth = self.frame.width / 2
            let halfHeight = self.frame.height / 2
            if imagePaths.count == 1 {
                let imageView = UIImageView()
                imageView.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height)
                imageView.layer.masksToBounds = true
                imageView.layer.cornerRadius = imageView.frame.width / 2
                imageView.sd_setImageWithURL(NSURL(string: imagePaths[0]))
                imageView.backgroundColor = RGB(237, g: 237, b: 237)
                self.addSubview(imageView)
            } else if imagePaths.count == 2 {
                var imageView = UIImageView()
                imageView.frame = CGRect(x: 0, y: halfHeight / 2, width: halfWidth, height: halfHeight)
                imageView.layer.masksToBounds = true
                imageView.layer.cornerRadius = imageView.frame.width / 2
                imageView.sd_setImageWithURL(NSURL(string: imagePaths[0]))
                imageView.backgroundColor = RGB(237, g: 237, b: 237)
                self.addSubview(imageView)
                
                imageView = UIImageView()
                imageView.frame = CGRect(x: halfWidth, y: halfHeight / 2, width: halfWidth, height: halfHeight)
                imageView.layer.masksToBounds = true
                imageView.layer.cornerRadius = imageView.frame.width / 2
                imageView.sd_setImageWithURL(NSURL(string: imagePaths[1]))
                imageView.backgroundColor = RGB(237, g: 237, b: 237)
                self.addSubview(imageView)
            } else if imagePaths.count == 3 {
                var imageView = UIImageView()
                imageView.frame = CGRect(x: halfWidth / 2, y: 0, width: halfWidth, height: halfHeight)
                imageView.layer.masksToBounds = true
                imageView.layer.cornerRadius = imageView.frame.width / 2
                imageView.sd_setImageWithURL(NSURL(string: imagePaths[0]))
                imageView.backgroundColor = RGB(237, g: 237, b: 237)
                self.addSubview(imageView)
                
                imageView = UIImageView()
                imageView.frame = CGRect(x: 0, y: halfHeight, width: halfWidth, height: halfHeight)
                imageView.layer.masksToBounds = true
                imageView.layer.cornerRadius = imageView.frame.width / 2
                imageView.sd_setImageWithURL(NSURL(string: imagePaths[1]))
                imageView.backgroundColor = RGB(237, g: 237, b: 237)
                self.addSubview(imageView)
                
                imageView = UIImageView()
                imageView.frame = CGRect(x: halfWidth, y: halfHeight, width: halfWidth, height: halfHeight)
                imageView.layer.masksToBounds = true
                imageView.layer.cornerRadius = imageView.frame.width / 2
                imageView.sd_setImageWithURL(NSURL(string: imagePaths[2]))
                imageView.backgroundColor = RGB(237, g: 237, b: 237)
                self.addSubview(imageView)
            } else if imagePaths.count >= 4 {
                var imageView = UIImageView()
                imageView.frame = CGRect(x: 0, y: 0, width: halfWidth, height: halfHeight)
                imageView.layer.masksToBounds = true
                imageView.layer.cornerRadius = imageView.frame.width / 2
                imageView.sd_setImageWithURL(NSURL(string: imagePaths[0]))
                imageView.backgroundColor = RGB(237, g: 237, b: 237)
                self.addSubview(imageView)
                
                imageView = UIImageView()
                imageView.frame = CGRect(x: halfWidth, y: 0, width: halfWidth, height: halfHeight)
                imageView.layer.masksToBounds = true
                imageView.layer.cornerRadius = imageView.frame.width / 2
                imageView.sd_setImageWithURL(NSURL(string: imagePaths[1]))
                imageView.backgroundColor = RGB(237, g: 237, b: 237)
                self.addSubview(imageView)
                
                imageView = UIImageView()
                imageView.frame = CGRect(x: 0, y: halfHeight, width: halfWidth, height: halfHeight)
                imageView.layer.masksToBounds = true
                imageView.layer.cornerRadius = imageView.frame.width / 2
                imageView.sd_setImageWithURL(NSURL(string: imagePaths[2]))
                imageView.backgroundColor = RGB(237, g: 237, b: 237)
                self.addSubview(imageView)
                
                imageView = UIImageView()
                imageView.frame = CGRect(x: halfWidth, y: halfHeight, width: halfWidth, height: halfHeight)
                imageView.layer.masksToBounds = true
                imageView.layer.cornerRadius = imageView.frame.width / 2
                imageView.sd_setImageWithURL(NSURL(string: imagePaths[3]))
                imageView.backgroundColor = RGB(237, g: 237, b: 237)
                self.addSubview(imageView)
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initSelf()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.initSelf()
    }
    
    private func initSelf() {
        
    }
}
